package view.components{
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.BulkProgressEvent;
	import br.com.stimuli.loading.loadingtypes.LoadingItem;
	
	import events.AnimationEvent;
	
	import manager.LoggerManager;
	
	import scaleform.clik.controls.Button;
	
	import sunix.animationPlayer.data.FrameData;
	
	import utils.BitmapUtils;
	import utils.FileUtil;
	import utils.SWFCompilerUtil;
	import utils.SpriteSheetUtil;

	[Event(name="preview", type="events.AnimationEvent")]
	[Event(name="compiled", type="events.AnimationEvent")]
	[Event(name="play", type="events.AnimationEvent")]
	public class AnimationOperator extends Sprite{
		
		public var preview:Button;	  //预览按钮
		public var compiled:Button;	   //编译按钮
		public var frames:Vector.<FrameData>;  //处理后的图片数组
		
		protected var isPreview:Boolean = false;
		
		public function AnimationOperator(){
			addEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
		}
		
		public function compiledFile(name:String,fps:uint,outPath:String,compressPath:String):void{
			SpriteSheetUtil.genEffectBitmap(name, fps, frames, outPath);
			swfCompiler(outPath,name,compressPath);
		}
		
		public function reset():void{
			isPreview = false;
		}
		
		public function getOriginalFiles(file:File):void{
			if(!file){
				LoggerManager.error("请选择资源文件路径");
				return;
			}
			if(!file.isDirectory){
				LoggerManager.error("资源路径不存在");
				return;
			}
			LoggerManager.log("获取原始资源文件完成");
			filterPngFile(file);
		}
		
		protected function onAddedToStage(event:Event):void{
			eventInit();
		}
		
		private function eventInit():void{
			preview.addEventListener(MouseEvent.CLICK,onPreview);
			compiled.addEventListener(MouseEvent.CLICK,onCompiled);
		}
		
		protected function onCompiled(event:MouseEvent):void{
			if(!isPreview){
				LoggerManager.error("请先预览文件");
				return;
			}
			dispatchEvent(new AnimationEvent(AnimationEvent.COMPILED));

		}
		
		protected function swfCompiler(outPath:String,name:String,compressPath:String):void{
			var savepath:String = outPath + "/" + name;
			var output:String = compressPath + "/";
			SWFCompilerUtil.instance.delayStart(SWFCompilerUtil.CMDTYPE_FILES,output,savepath+".png",[savepath+".xml"]);
		}
		
		protected function onPreview(event:MouseEvent):void{
			dispatchEvent(new AnimationEvent(AnimationEvent.PREVIEW));
		}

		protected function filterPngFile(file:File):void{
			var originalFiles:Array = file.getDirectoryListing();
			var processFile:Array = FileUtil.filterFileByExtendName(originalFiles,'png');
			if(!processFile.length){
				LoggerManager.error("资源路径中不包含有效文件，请重新选择");
				return;
			}
			LoggerManager.log("筛选png图片完成，共计：",processFile.length,"张图片");
			var sortFile:Array = FileUtil.sortFileByNumber(processFile,0, processFile.length-1);
			LoggerManager.log("图片排序完成");
			loaderImage(sortFile);
		}
		
		protected function loaderImage(array:Array):void{
			var loader:BulkLoader = BulkLoader.createUniqueNamedLoader();
			loader.addEventListener(BulkProgressEvent.COMPLETE,onComplete);
			var length:uint = array.length;
			var file:File;
			for(var i:uint = 0 ; i < length; i++){
				file = array[i];
				loader.add(file.nativePath);
			}
			loader.start();
		}
		
		protected function onComplete(event:BulkProgressEvent):void{
			LoggerManager.log("图片加载完成");
			var loader:BulkLoader = event.target as BulkLoader;
			var array:Array = loader.items;
			var length:uint = array.length;
			var images:Vector.<BitmapData> = new Vector.<BitmapData>();
			var item:LoadingItem;
			for(var i:uint = 0; i< length; i++){
				item = array[i];
				var bmd:BitmapData = loader.getBitmapData(item.url.url);
				images.push(bmd);
			}
			frames = BitmapUtils.cropBitmaps(images);
			dispatchEvent(new AnimationEvent(AnimationEvent.PLAY));
			isPreview = true;
		}
	}
}